package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.UpPasswordModel;
import com.banshuibao.bsb_android.util.MD5;
import com.banshuibao.bsb_android.util.XToast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UpPasswordActivity extends BaseActivity implements View.OnClickListener {

    private TextView commit;
    private EditText password_one, password_two, password_three;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_up_password);
        initView();
    }

    private void initView() {
        setTitleText("修改密码");
        commit = (TextView) findViewById(R.id.menu);
        commit.setVisibility(View.VISIBLE);
        commit.setText("提交");
        commit.setOnClickListener(this);
        password_one = (EditText) findViewById(R.id.password_one);
        password_two = (EditText) findViewById(R.id.password_two);
        password_three = (EditText) findViewById(R.id.password_three);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.menu:
                String pass1 = password_one.getText().toString().trim();
                String pass2 = password_two.getText().toString().trim();
                String pass3 = password_three.getText().toString().trim();
                if (!TextUtils.isEmpty(pass1) && isPassword(pass1)) {
                    if (!TextUtils.isEmpty(pass2) && isPassword(pass2)) {
                        if (!TextUtils.isEmpty(pass3) && isPassword(pass3)) {
                            if (pass2.equals(pass3)) {
                                try {
                                    showProgressDialog();
                                    setPost(pass1, pass2);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            } else {
                                XToast.showToast(this, "两次输入的密码不一致");
                            }
                        } else {
                            XToast.showToast(this, "请确认新密码");
                        }
                    } else {
                        XToast.showToast(this, "请输入新密码");
                    }
                } else {
                    XToast.showToast(this, "请输入正确的密码格式");
                }
                break;
        }
    }

    private void setPost(String p1, String pass2) throws Exception {
        UpPasswordModel.body body = new UpPasswordModel.body();
        body.setPwd(MD5.encryptMD5ToString(p1.getBytes()));
        body.setNewPwd(MD5.encryptMD5ToString(pass2.getBytes()));
        HttpUtil.doPost(Constants.Url.UP_PASSWORD, body, new HttpResponse(this) {
            @Override
            public void onResult(Object obj) {
//                String s = (String) obj;
//                LogUtil.e(s);
                cancelProgressDialog();
                XToast.showToast(UpPasswordActivity.this, "密码修改成功");
                finish();
            }
        });
    }

    public boolean isPassword(String password) {
        Pattern pattern = Pattern.compile("[0-9a-zA-Z\\d]{6,15}");
        Matcher matcher = pattern.matcher(password);
        return matcher.matches();
    }
}
